<?php
namespace Ana\AgendaBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\HttpFoundation\Response;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use \DateTime;

class CalendarioController extends BaseController{
    
    /**
    * @Route("/calendario",name="calendario_home")
    * @Method("GET")
    *
    */
    public function calendarioAction(){
        return $this->render('AnaAgendaBundle:Calendario:calendario_home.html.php');
    }
    
    /**
    * @Route("/calendario/jornadas",name="jornadas_con_turnos",defaults={"_format": "json"})
    * @Method("GET")
    *
    */
    public function jornadaTurnosAction(Request $request){
        $jornadas = $this->getDoctrine()->getRepository('AnaAgendaBundle:Jornada')->jornadasConTurnos($request->get('desde'),$request->get('hasta'));
        $jornadas_array = array();
        foreach ($jornadas as $jornada){
            $classes = $jornada->getCompleto()?array('jornada','jornada-completa'):'jornada';
            $title  = $jornada->getCompleto()?"Completo ({$jornada->getCantTurnos()})":"{$jornada->getCantTurnos()} turno/s";
            $jornadas_array[]= array(
                        "id"=>$jornada->getId(),
                        "title"=>$title,
                        "start"=>$jornada->getFecha()->format(DateTime::ISO8601),
                        "finish"=>$jornada->getFecha()->format(DateTime::ISO8601),
                        "allDay"=>true,
                        "className"=>$classes
            );
        }
        return $this->successAjaxResponse($jornadas_array);
    }
    
    /**
    * @Route("/calendario/turnos.{_format}",name="turnos_calendario",defaults={"_format": "json"})
    * @Method("GET")
    *
    */
    public function turnosCalendarioAction(Request $request){
        $turnos = $this->getDoctrine()->getRepository('AnaAgendaBundle:Turno')->turnosParaCalendarioDTO($request->get('desde'),$request->get('hasta'));
        $turnos_array = array();
        foreach ($turnos as $turno){
            $turnos_array[]= array(
                            "id"=>$turno['id'],
                            "title"=>$turno['title'],
                            "start"=>$turno['start'],
                            "end"=>$turno['finish'],
                            "className"=>"jornada"
            );
        }
        return $this->successAjaxResponse($turnos_array);
    }
    
}